<?php
	class LightComanda {				
				
		var $version;
		var $moneda;
        
		function __construct() {
			//fast :D
		}
		
		public static function queryLightLoadByID($db, $idComanda, $idClient, $monedaType) {
		    $query = $db->prepare("SELECT version, " . $monedaType . " 
	            					FROM comanda
	        						WHERE id = :id_comanda
	            					AND id_client = :id_client AND deleted = :deleted");
		    
		    $query->bindParam(':id_comanda', 	$idComanda);
		    $query->bindParam(':id_client', 	$idClient);
		    $query->bindValue(':deleted', 		CT_NOT_DELETED);
		    
	        if ($query->execute() === FALSE) { 
                $errInfo = $query->errorInfo();
                $errInfo = $errInfo[2];
	        	$query->closeCursor();
	        	throw new QueryLightLoadComandaException("Faild loading comanda with id " . $idComanda . "and id client = " . $idClient . $errInfo);
	        }
	            
	    	$data = $query->fetch(PDO::FETCH_ASSOC); 
	        
	        $comanda = new LightComanda();
            $comanda->version = $data['version'];
            $comanda->moneda = $data[$monedaType];

	        $query->closeCursor();
	    	return $comanda;			
		}
		
		public function updateStare($db, $campStare, $valoareCampStare, $idComanda) {
			$this->version++;
			
			if ($campStare == "stare_plata_exec")
				$query = $db->prepare("UPDATE `comanda`		
											SET		stare_plata_exec = :platit
											WHERE 	id = :id_comanda
                                                AND version < :version");
			else if ($campStare == "stare_incasare_sol")
				$query = $db->prepare("UPDATE `comanda`		
											SET		stare_incasare_sol = :platit
											WHERE 	id = :id_comanda
                                                AND version < :version");
			
			$query->bindParam(':platit', $valoareCampStare);
            $query->bindParam(':id_comanda', $idComanda);
			$query->bindParam(':version', $this->version);
									
			if ($query->execute() === FALSE) {
                $errInfo = $query->errorInfo();
                $errInfo = $errInfo[2];
				$query->closeCursor();				
				throw new QueryUpdateStareException("Error updating status to platit for comanda with id " . $idComanda . $errInfo);
			}
			
			$query->closeCursor();
	        return CT_RESULT_OK;
		}
		
	}
?>